/*
 * LevelupRewardResource.cpp
 *
 *  Created on: 2013. 11. 11.
 *      Author: mega@gamevil.com
 */

#include "../User.h"
#include "../NUtil/Utils.h"
#include "LevelupRewardResource.h"

#include <Database/Static.h>

namespace ProjectZ { namespace NResource {

LevelupRewardResource::LevelupRewardData::LevelupRewardData()
{
	clsFlyTree.clear();
}

LevelupRewardResource::LevelupRewardData::~LevelupRewardData()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<LEVELUPREWARDTREE::value_type>);
}

LevelupRewardResource::LevelupRewardResource()
{
	pLevelupRewardAD	= new LevelupRewardData;
	pLevelupRewardIOS	= new LevelupRewardData;
}

LevelupRewardResource::~LevelupRewardResource()
{
	delete pLevelupRewardAD;
	delete pLevelupRewardIOS;
}

void
LevelupRewardResource::LevelupRewardData::Clear()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<LEVELUPREWARDTREE::value_type>);
	clsFlyTree.clear();
}

bool
LevelupRewardResource::LevelupRewardData::LoadResource(int company, int salecode)
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("SELECT NATIONAL_CODE, level, gold FROM resource_levelup_reward WHERE company=%d and sale_code=%d", company, salecode);

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if (clsAuto->GetErrorIdx() != 0)
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	Clear();

	while (clsAuto->Next())
	{
		::Toolkit::Str<3> NATIONAL_CODE;
		Flyweight * pFly = new Flyweight;

		clsAuto->GetRecord() >> NATIONAL_CODE >> pFly->_LEVEL >> pFly->_GOLD;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}

		clsFlyTree[pFly->_LEVEL] = pFly;
	}

	return true;
}

bool
LevelupRewardResource::LoadResource()
{
	if (false == pLevelupRewardAD->LoadResource(MARKET_GOOGLE, SC_KAKAO_GOOGLE))
		return false;

	if (false == pLevelupRewardIOS->LoadResource(MARKET_IOS, SC_KAKAO_IOS))
		return false;

	return true;
}

LevelupRewardResource::LevelupRewardData::Flyweight *
LevelupRewardResource::LevelupRewardData::GetFly(int level)
{
	auto iter = clsFlyTree.find(level);
	if (iter == clsFlyTree.end())
		return NULL;

	return iter->second;
}

LevelupRewardResource::LevelupRewardData::Flyweight *
LevelupRewardResource::GetFly(User * pUser)
{
	int level = pUser->GetCharacterInfo().level;

	NUtil::MarketType marketType = NUtil::GetMarketType(pUser->GetCompany(), pUser->GetSaleCode());

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		return pLevelupRewardAD->GetFly(level);

	case NUtil::MarketType::IOS_KAKAO:
		return pLevelupRewardIOS->GetFly(level);

	default:
		return NULL;
	}

	return NULL;
}

} /*NResource*/ } /*ProjectZ*/
